﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Waiter.Common;

namespace Waiter.Controler
{
    class TableCtrl
    {
        public List<TABLE> fsGetTableListForDisplay(string areaCode)
        {
            List<TABLE> resultList = new List<TABLE>();
            try
            {
                if (areaCode != null)
                {
                    resultList = (from s in CommonGlobalVariable.globalDataContext.TABLEs
                                  where s.AREA_CODE == areaCode
                                    && (s.STATUS_CODE == (int)TableStatus.EMPTY || s.STATUS_CODE == (int)TableStatus.BUSY)
                                    && s.DEL_FLG == CommonConstants.FLAG_OFF
                                  orderby s.TABLE_NAME ascending
                                  select s).ToList<TABLE>();
                }
                else
                {
                    resultList = (from s in CommonGlobalVariable.globalDataContext.TABLEs
                                  where (s.STATUS_CODE == (int)TableStatus.EMPTY || s.STATUS_CODE == (int)TableStatus.BUSY)
                                    && s.DEL_FLG == CommonConstants.FLAG_OFF
                                  orderby s.TABLE_NAME ascending
                                  select s).ToList<TABLE>();
                }
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                           "TableCtrl.fsGetTableListForDisplay",
                           CommonUtils.fsParamListToString(new object[1] { areaCode }),
                           ex.ToString());
            }
            return resultList;
        }

        public List<TABLE> fsGetAllTableList(string areaCode)
        {
            List<TABLE> resultList = new List<TABLE>();
            try
            {
                if (areaCode != null)
                {
                    resultList = (from s in CommonGlobalVariable.globalDataContext.TABLEs
                                  where s.AREA_CODE == areaCode && s.DEL_FLG == CommonConstants.FLAG_OFF
                                  orderby s.TABLE_NAME ascending
                                  select s).ToList<TABLE>();
                }
                else
                {
                    resultList = (from s in CommonGlobalVariable.globalDataContext.TABLEs
                                  where (s.STATUS_CODE == (int)TableStatus.EMPTY || s.STATUS_CODE == (int)TableStatus.BUSY)
                                  orderby s.TABLE_NAME ascending
                                  select s).ToList<TABLE>();
                }
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                           "TableCtrl.fsGetAllTableList",
                           CommonUtils.fsParamListToString(new object[1] { areaCode }),
                           ex.ToString());
            }
            return resultList;
        }
        public TABLE fsFindTableByCode(string tableCode)
        {
            TABLE resultItem = null;
            try
            {
                resultItem = CommonGlobalVariable.globalDataContext.TABLEs.FirstOrDefault(q => q.CODE == tableCode);
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                           "TableCtrl.fsFindTableByCode",
                           CommonUtils.fsParamListToString(new object[1] { tableCode }),
                           ex.ToString());
            }
            return resultItem;
        }

        public ResultStatus fsUpdateTableStatus(string tableCode, int status)
        {
            try
            {
                TABLE item = this.fsFindTableByCode(tableCode);
                item.STATUS_CODE = status;
                CommonGlobalVariable.globalDataContext.SubmitChanges();
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                           "TableCtrl.fsUpdateTableStatus",
                           CommonUtils.fsParamListToString(new object[2] { tableCode, status }),
                           ex.ToString());
                return ResultStatus.ERROR;
            }

            return ResultStatus.SUCCESS;
        }
        public ResultStatus fsUpdateTableName(string tableCode, string tabeName)
        {
            try
            {
                TABLE item = this.fsFindTableByCode(tableCode);
                item.TABLE_NAME = tabeName;
                CommonGlobalVariable.globalDataContext.SubmitChanges();
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                           "TableCtrl.fsUpdateTableName",
                           CommonUtils.fsParamListToString(new object[2] { tableCode, tabeName }),
                           ex.ToString());
                return ResultStatus.ERROR;
            }

            return ResultStatus.SUCCESS;
        }
        public ResultStatus fsInsertTable(TABLE item)
        {
            try
            {
                while (fsFindTableByCode(item.CODE) != null)
                {
                    item.CODE = CommonUtils.fsCreateNewCode();
                }
                CommonGlobalVariable.globalDataContext.TABLEs.InsertOnSubmit(item);
                CommonGlobalVariable.globalDataContext.SubmitChanges();
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                           "TableCtrl.fsInsertTable",
                           CommonUtils.fsParamListToString(new object[1] { item }),
                           ex.ToString());
                return ResultStatus.ERROR;
            }
            return ResultStatus.SUCCESS;
        }

        public ResultStatus fsDeleteTable(string tableCode)
        {
            try
            {
                TABLE item = fsFindTableByCode(tableCode);
                item.DEL_FLG = CommonConstants.FLAG_ON;
                CommonGlobalVariable.globalDataContext.SubmitChanges();
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                           "TableCtrl.fsDeleteTable",
                           CommonUtils.fsParamListToString(new object[1] { tableCode }),
                           ex.ToString());
                return ResultStatus.ERROR;
            }
            return ResultStatus.SUCCESS;
        }
        public string fsGetNewTableName(string areaCode, string areaSign)
        {
            string tableName = null;
            try
            {
                int tableCount = CommonGlobalVariable.globalDataContext.TABLEs.Count(q=>q.AREA_CODE == areaCode
                                                                                    && q.DEL_FLG == CommonConstants.FLAG_OFF);
                TABLE item = null;
                do
                {
                    tableCount++;
                    tableName = areaSign + tableCount;
                    item = CommonGlobalVariable.globalDataContext.TABLEs.FirstOrDefault(q =>q.AREA_CODE == areaCode 
                                                                                        && q.TABLE_NAME == tableName
                                                                                        && q.DEL_FLG == CommonConstants.FLAG_OFF);
                } while (item != null);
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                           "TableCtrl.fsGetNewTableName",
                           CommonUtils.fsParamListToString(new object[2] { areaCode, areaSign }),
                           ex.ToString());
            }
            return tableName;
        }

    }
}
